package com.yixing.tech.wcs.controller;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.wcs.service.IBoxTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping(value = "/tbea/ic/wcs/box/interaction")
@Slf4j
public class BoxInteractionController extends BaseControl {

    @Resource
    private IBoxTaskService boxTaskService;

    @PostMapping("/finishHandling")
    public Rs<Object> finishHandling(@RequestBody Map<String, Object> map) throws BusinessException {
        if(ObjectUtils.isEmpty(map) || ObjectUtils.isEmpty(map.get("podCode"))) {
            throw new BusinessException("podCode参数不能为空!");
        }
        boxTaskService.boxHandlingFinish(map.get("podCode").toString());
        return Rs.ok();
    }

    /**
     * @param map
     * {
     *     "podCode": "料箱号",
     *     "interactionState": "料箱交互状态字典id"
     * }
     * */
    @PostMapping("/updateBoxState")
    public Rs<Object> updateBoxState(@RequestBody Map<String, Object> map) throws BusinessException {

        if(ObjectUtils.isEmpty(map) || ObjectUtils.isEmpty(map.get("podCode"))) {
            throw new BusinessException("podCode参数不能为空!");
        }
        if(ObjectUtils.isEmpty(map.get("interactionState"))) {
            throw new BusinessException("interactionState参数不能为空!");
        }

        log.info("请求更新料箱交互状态:podCode[{}],state[{}]", map.get("podCode").toString(), map.get("interactionState").toString());
        boxTaskService.updateBoxState(map.get("podCode").toString(), map.get("interactionState").toString());
        return Rs.ok();
    }
}
